<template>
  <div class="p-2" style="width: 280px">
    <div v-if="isLoading" class="text-center mt-4">
      <a-spin/>
    </div>
    <a-alert v-if="error" :description="error.message" type="error"/>
  </div>
</template>
<script setup>
const route = useRoute();
const {data, isLoading, isFinished, error} = useApi({
  method: 'POST',
  url: '/login/wx-open',
  data: {
    code: route.query.code
  },
  showError: false
});

watchEffect(() => {
  if (isFinished.value) {
    if (error.value != null) {
      return;
    }
    const {binding, token} = data.value;
    window.parent.postMessage({
      type: 'wx-open-login',
      token,
      binding
    }, "*");
  }
});


</script>
